#include "ccube.h"
// Les indices de vertices
static unsigned int IndiceArrayCube[] = {
  0,1,2,2,1,3,
  4,5,6,6,5,7,
  3,1,5,5,1,7,
  0,2,6,6,2,4,
  6,7,0,0,7,1,
  2,3,4,4,3,5
};

static float CoordTex2D[] = {
  0.0, 0.0,
  0.0, 1.0,
  1.0, 0.0,
  1.1, 1.1,
  0.0, 0.0,
  0.0, 1.0,
  1.0, 0.0,
  1.1, 1.1
};

CCube::CCube()
{
    InitializeCube();
}

CCube::CCube(string sName) {
    InitializeCube();
    ShaderObject = new CShaderObject (sName+".vert", sName+".frag");
}

CCube::CCube(string sNameVertex, string sNameFragment) {
    InitializeCube();
    ShaderObject = new CShaderObject (sNameVertex, sNameFragment);
}

CCube::CCube(const CCube &copie)
{
    eCategory = copie.eCategory;
    piPosition = new int [3];
    piPosition[0] = copie.piPosition[0];
    piPosition[1] = copie.piPosition[1];
    piPosition[2] = copie.piPosition[2];
    ShaderObject = new CShaderObject (*(copie.ShaderObject));
    pfCubeArray = new float[24];
    for (int i=0 ; i<24 ; ++i) {
        pfCubeArray[i] = copie.pfCubeArray[i];
    }
}

CCube::~CCube()
{
    delete[] piPosition;
    delete[] pfCubeArray;
    delete ShaderObject;
}

void CCube::InitializeCube() {
    piPosition = new int [3];
    piPosition[0] = 0;
    piPosition[1] = 0;
    piPosition[2] = 0;
    ShaderObject = NULL;
    pfCubeArray = new float [24];
    pfCubeArray[0] = -1.0f;
    pfCubeArray[1] = 1.0f;
    pfCubeArray[2] = -1.0f;

    pfCubeArray[3] = -1.0f;
    pfCubeArray[4] = -1.0f;
    pfCubeArray[5] = -1.0f;

    pfCubeArray[6] = -1.0f;
    pfCubeArray[7] = 1.0f;
    pfCubeArray[8] = 1.0f;

    pfCubeArray[9] = -1.0f;
    pfCubeArray[10] = -1.0f;
    pfCubeArray[11] = 1.0f;

    pfCubeArray[12] = 1.0f;
    pfCubeArray[13] = 1.0f;
    pfCubeArray[14] = 1.0f;

    pfCubeArray[15] = 1.0f;
    pfCubeArray[16] = -1.0f;
    pfCubeArray[17] = 1.0f;

    pfCubeArray[18] = 1.0f;
    pfCubeArray[19] = 1.0f;
    pfCubeArray[20] = -1.0f;

    pfCubeArray[21] = 1.0f;
    pfCubeArray[22] = -1.0f;
    pfCubeArray[23] = -1.0f;

    eCategory = Cube::TERRE;

}

void CCube::setType(int iType) {
    eCategory = iType;
}


void CCube::setShader(string sName) {
    if (ShaderObject != NULL) {
        delete ShaderObject;
    }
    ShaderObject = new CShaderObject (sName+".vert", sName+".frag");
}

void CCube::setShader(string sNameVertex, string sNameFragment) {
    if (ShaderObject != NULL) {
        delete ShaderObject;
    }
    ShaderObject = new CShaderObject (sNameVertex, sNameFragment);
}

void CCube::setPosition(int _iX, int _iY, int _iZ) {
    if (piPosition[0] != _iX) {
        piPosition[0] = _iX;
        for (int i=0 ; i < 24 ; i+=3) {
            pfCubeArray[i] += _iX*2;
        }
    }

    if (piPosition[1] != _iY) {
        piPosition[1] = _iY;
        for (int i=1 ; i < 24 ; i+=3) {
            pfCubeArray[i] += _iY*2;
        }
    }

    if (piPosition[2] != _iZ) {
        piPosition[2] = _iZ;
        for (int i=2 ; i < 24 ; i+=3) {
            pfCubeArray[i] += _iZ*2;
        }
    }
}

int* CCube::getPosition() const {
    return piPosition;

}

/**
  @brief Initialize the shader
  @return true if success
  */
void CCube::Initialize() {
    //--Avant l'initialisation
    ShaderObject->setActiveVAO(true) ;
  int iVertex = 0;
  for (int i = 0 ; i < 24 ; i+=3) {
      ShaderObject->addDonneVertex("qt_TexCoord0", CoordTex2D[iVertex], CoordTex2D[iVertex+1]);
      ShaderObject->addDonneVertex ("qt_Vertex",pfCubeArray[i], pfCubeArray[i+1], pfCubeArray[i+2]) ;
      iVertex += 2;
  }
  ShaderObject->addDonneUniform("iType", 1, GL_INT,  &eCategory);


  for (int i = 0 ; i < 36 ; i++) {
    ShaderObject->addIndex(IndiceArrayCube[i]) ;
  }

    //A la fin quand tout est enregistré
    ShaderObject->initObject() ;
}

void CCube::addMatrix(const string &_NameMatrix, MatriceGL *_Matrix) {
    ShaderObject->addDonneMatrix(_NameMatrix, 4, 4, _Matrix->getData());
}

/**
  @brief Draw the cube
  */
void CCube::Play() {
    ShaderObject->Draw(GL_TRIANGLES);
}
